var code;
var tel;

$(function(){ 
    //清缓存 
    localStorage.clear();
    sessionStorage.clear();

    //通过扫二维码获取相应渠道表识
    var QRCode;
    QRCode = getQueryStr("channel");

    if(QRCode==""){
        QRCode = "";
    }else if(QRCode !== ""){
        QRCode = getQueryStr("channel");
    }
    //存储二维码类别
    sessionStorage.setItem("QRCodeHistory",QRCode);

    // 验证前判断是否存在手机号信息
    $(".information-obtain").on("click",function(){
       var telphoneNumber =  $("input[type='tel']").val();
       if(telphoneNumber == ""){
             swal({
                    title: "请输入手机号后进行验证!",
                    type: "warning",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                    });
            $('#myModal').modal('hide')
       }else if(telphoneNumber !== ""){
            $(".alert_input").val("");
            mtTest();
       }
    });
    
    // 输入验证码后点击确认
    $(".alert_btn-confirm").on("click",function(){
        tel = $("input[type='tel']").val();
        var code = $(".alert_input").val();
        $.ajax({
            type: "post",
            url: baseUrl+"/dingding-web/codeAction!validateCodeIsValid.action",
            dataType: "jsonp",
            async: false,
            jsonp: "callback",
            jsonpCallback: "success_jsonp",
            data:{
                code:code
            },
            success: function(data){
                console.log(data);
                if(data.code == 200){
                    $('#myModal').modal('hide');
                    getValidateCode();
                    var count = 60;
                    var countdown =setInterval(Countdown,1000);
                    function Countdown(){
                        $(".information-hq").text(count+"s").attr("disabled",true);
                        count--;
                        if(count==0){
                            $(".information-hq").text("验证").removeAttr("disabled");
                            clearInterval(countdown);
                        }
                    };
                }
                else if(data.code == 205){
                    swal({
                        title: "输入验证码有误!",
                        type: "warning",
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "确定",
                    });
                    $('#myModal').modal('hide')
                }

            }
        });         
          
    });
   
    //input监听事件内容充满后变色
    $('input').bind('input propertychange', function() {
        tel = $("input[type='tel']").val();
        code = $("input[type='number']").val();
        if(tel!==""&&code!==""&&code.length==4&&tel.length==11){
            $(".btn_car").css("background","#FF4C4B");
            $(".btn_car").attr("disabled",false);
        }else if(tel==""||code==""||code.length<4){
            $(".btn_car").css("background","#C6C6C6");
            $(".btn_car").attr("disabled",true);
        } 
    });

    /*用户点击登陆前往对应的活动主页*/
    $(".btn_car").on("click",function(){
        tel = $("input[type='tel']").val();
        code = $("input[type='number']").val();
        if(tel && code){       
            $.ajax({
                type: "post",
                url:baseUrl+"/dingding-web/driverAction!webUserLogin.action",
                dataType: "jsonp",
                async: false,
                jsonp: "callback",
                jsonpCallback: "success_jsonp",
                data: {
                    telPhone:tel,
                    code:code
                },
                success: function(data){
                    console.log(data);
                    var userId = data.responseBody.userId;
                    console.log(userId);
                    // 认证状态 0:未注册过 1：审核中 2：审核通过 3：认证失败
                    if(data.responseBody.suvStatus == 2){
                        window.location.href="success.html";
                    }
                    if(data.responseBody.suvStatus == 3){
                        window.location.href="error.html?userId="+userId;

                    }
                    if(data.responseBody.suvStatus == 0){
                        window.location.href="driverInformation.html?userId="+userId;
                    }
                    if(data.responseBody.suvStatus == 1){
                        window.location.href="checking.html";
                    }
                }
            });
        }else{
            swal({
                title: "请输入手机号或验证码",
                type: "warning",
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
            });
        }       
    });

});

//封装选择驾驶证类型弹框方法
function mtTest(){
    $('#myModal').modal('show');
}

//点击验证图片变化
function clickImage(){
    var number = Math.random();
    $('.alert_img').attr("src",baseUrl+"/dingding-web/validateCode?str=" + number);

}
//获取手机验证短信封装函数
function getValidateCode(){
    $.ajax({
        type:"post",
         url: baseUrl+"/dingding-web/regActiveAction!getValidateCode.action",
         dataType:"jsonp",
         async:false,
         jsonp:"callback",
         jsonpCallback:"succees_jsonp",
         data:{
            tellphone:tel
         },
         success:function(data){
           if(data.responseBody=="error"){
                swal({
                    title: "手机验证码获取失败",
                    type: "warning",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                });
            }
         }

    });
}